package live.tag;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import live.common.Format;

import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;


public class PreAndNextDayTag extends TagSupport {

	private static final long serialVersionUID = 1057365263279823505L;
	private Format format = Format.getInstance();
	
	private String strTime;
	private String strHref;
	private String toCityName;
	private String fromCityName;

	public String getStrTime() {
		return strTime;
	}

	public void setStrTime(String strTime) throws JspException {
		this.strTime = (String) ExpressionEvaluatorManager.evaluate("strTime",
				strTime, Object.class, this, this.pageContext);
	}
	
	public String getStrHref() {
		return strHref;
	}

	public void setStrHref(String strHref) {
		this.strHref = strHref;
	}
	

	public String getToCityName() {
		return toCityName;
	}

	public void setToCityName(String toCityName) throws JspException {
		this.toCityName = (String) ExpressionEvaluatorManager.evaluate("toCityName",
				toCityName, Object.class, this, this.pageContext);
	}

	public String getFromCityName() {
		return fromCityName;
	}

	public void setFromCityName(String fromCityName) throws JspException {
		this.fromCityName = (String) ExpressionEvaluatorManager.evaluate("fromCityName",
				fromCityName, Object.class, this, this.pageContext);
	}

	@Override
	public int doEndTag() throws JspException {
		// TODO Auto-generated method stub
		try {
			Date date = format.stringToDate(strTime,"yyyy-MM-dd");
			Date today = format.stringToDate(format.dateFormatString(new Date(), "yyyy-MM-dd"), "yyyy-MM-dd");
			Calendar calendartoday = Calendar.getInstance();
			calendartoday.setTime(today);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			StringBuffer strBuffer = new StringBuffer();
			calendar.add(calendar.DAY_OF_WEEK,-1);
			if(calendar.getTimeInMillis()-calendartoday.getTimeInMillis()>=0){
				strBuffer.append("<<&nbsp;<a href=\"").append(strHref).append(format.dateFormatString(calendar.getTime(), "yyyy-MM-dd"))
				.append("&fromCityName=").append(Format.getInstance().encodeUTF(fromCityName)).append("&toCityName=")
				.append(Format.getInstance().encodeUTF(toCityName)).append("\">向前一天</a>(")
				.append(format.dateFormatString(calendar.getTime(), "MM-dd")).append(")&nbsp;&nbsp;");
			}
				calendar.add(calendar.DAY_OF_WEEK,+2);
				strBuffer.append("(").append(format.dateFormatString(calendar.getTime(), "MM-dd")).append(")")
				.append("<a href=\"").append(strHref).append(format.dateFormatString(calendar.getTime(), "yyyy-MM-dd")).append("&fromCityName=")
				.append(Format.getInstance().encodeUTF(fromCityName)).append("&toCityName=").append(Format.getInstance().encodeUTF(toCityName))
				.append("\">向后一天</a>").append("&nbsp;>>");
			pageContext.getOut().write(strBuffer.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return super.doEndTag();
	}
	
	
	
}
